<?php
namespace App\Http\Controllers;

use App\Http\Model\FormatCheck;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\ConfigServices;
use App\Http\Services\NationServices;
use App\Http\Services\UserServices;
use Illuminate\Http\Request;

//国庆
class NationController extends Controller
{

    protected $configServices;
    protected $userServices;
    protected $nationServices;

    public function __construct()
    {
        parent::__construct();
        $this->configServices = new ConfigServices();
        $this->userServices = new UserServices();
        $this->nationServices = new NationServices();
    }


    public function assignAction($body)
    {
        if ($body['consumer_type'] == 1) {
            $body_date = $body['create'];//送礼物
        } else if ($body['consumer_type'] == 2) {
            $body_date = $body['pay_timestamp']; //充值
        } else {
            $body_date = $body['change_timestamp']; //礼包
        }

        $consumer_date = $body_date ?? time();
        $config = $this->configServices->getConfig(6);
        $checkDate = $this->configServices->checkConfigDate($config['nation_date'], $consumer_date);
        if (!$checkDate) {
            write_log('moonConsumer', 'timeError', $body);//活动时间错误
            return false;
        }

        if ($body['consumer_type'] == 1) {
            $this->listenUserCollectGift($body);
        }
        if ($body['consumer_type'] == 2) {
            $this->listenUserRechareGiftBag($body);// 充值
        }
        if ($body['consumer_type'] == 3) {
            //用户购买中秋礼包
            $this->listenUserPayTicket($body);
        }
        return true;
    }


    //监听用户充值
    public function listenUserRechareGiftBag($body)
    {
        $recharge_arr = [6, 30, 98];
        $user_id = $body['user_id'] ?? 0;
        $price = $body['price'] / 100;
        //判定用户充值等级
        if (!in_array($price, $recharge_arr)) {
            write_log('nationConsumer', 'rechargePriceError', $body);//不达标
            return false; //充值不达标
        }
        $recharge_arr = array_flip($recharge_arr);
        $rank = $recharge_arr[$price] + 1;
        //验证用户是否当天领取过该礼包
        $checkRecharege = $this->nationServices->checkRecharge($user_id, $rank);
        if (!$checkRecharege) {
            write_log('nationConsumer', 'repetitionRecharge', $user_id . '-' . $rank);//重复充值等级
            return false;
        }
        write_log('nationConsumer', 'rechargeSuccess', $user_id . '-' . $rank);
        return true;
    }

    //查看当日用户是否领取礼包
    public function getUserRechargeBag(Request $request)
    {
        $user_id = $request->input('user_id');
        $res = $this->nationServices->getRechargeLog($user_id);
        OutJson::outJsonResult(Hint::SUCCESS, $res);
    }

    //用户点击领取礼包
    /*code
     * 200 成功
     * 201 该等级未充值
     * 202已经领取
     * 203/领取失败
     * */
    public function clickBag(Request $request)
    {
        $rank = $request->input('rank') ?? 0;
        $user_id = $request->input('user_id') ?? 0;
        $res = $this->nationServices->giftBagClickGet($user_id, $rank);
        OutJson::toJson($res['code'], $res['msg'], $res['data']);
    }

    public function listenUserCollectGift($body)
    {
        //查看用户 婚纱 or kiss
        $collect_gift = [69, 59];
        $gift_id = $body['gift_id'];
        $user_id = $body['user_id'];
        $room_id = $body['room_id'] ?? 0;
        $to_user_ids = $body['to_user_id_array'];
        $gift_num = $body['gift_num'];
        if (in_array($gift_id, $collect_gift)) {
            //婚纱活动礼品
            $this->nationServices->collectVeil($to_user_ids, $gift_id, $gift_num);
        }
        //赠送礼物排行
        return $this->nationServices->giftRank($gift_id, $user_id, $to_user_ids, $room_id, $gift_num);
    }

    public function getUserGiftLog(Request $request)
    {
        $id = $request->input('user_id') ?? 0;
        $res = $this->nationServices->getSendGiftLog($id);
        OutJson::outJsonResult(Hint::SUCCESS, $res);
    }

    public function clickRing(Request $request)
    {
        $id = $request->input('user_id') ?? 0;
        $res = $this->nationServices->clickRing($id);
        OutJson::toJson($res['code'], $res['msg']);
    }

    //兑换
    public function convertFlag(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $type = $request->input('type') ?? 1; //1 亲密值 2名片
        $num = abs($request->input('num')) ?? 1; //输入数量
        $res = $this->nationServices->convert($user_id, $type, $num);
        OutJson::toJson($res['code'], $res['msg']);
    }

    //用户购买抽奖券
    public function listenUserPayTicket($body)
    {
        $user_id = $body['user_id'];
        $num = $body['num'] ?? 0;
        return $this->nationServices->payTicket($user_id, $num);
    }

    public function consumerTicket(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $ticket_num = $request->input('ticket_num') ?? 1;
        $res = $this->nationServices->checkTicket($user_id, $ticket_num);
        OutJson::toJson($res['code'], $res['msg'], $res['data']);
    }

    public function getMallLog(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $res = $this->nationServices->getDialAwardLog($user_id);
        OutJson::toJson(Hint::SUCCESS, '成功', $res);
    }

    //兑换旗子
    public function fieldConvertFlag(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $res = $this->nationServices->editFeildNum($user_id);
        OutJson::toJson($res['code'], $res['msg']);
    }



    public function showFlagField(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $getFlagFeildNum = $this->nationServices->getFlagFeildNum($user_id);
        $getGridNum['ticket'] = $this->nationServices->getGridNum($user_id);
        $lingqu['log'] = $this->nationServices->getGridLog($user_id);
        $intimacy = $this->nationServices->showUserIntimacy($user_id);
        $arr = array_merge($getFlagFeildNum, $getGridNum);
        $res = array_merge($arr, $lingqu);
        $intimacy_num = 0;
        if ($intimacy) {
            $intimacy_num = $intimacy->consumer_intimacy_num;
        }else{
            $intimacy['real_num'] = 0;
        }
        $res['rank'] = $this->nationServices->getGiftRank($user_id);
        $res['intimacy'] = 100 - $intimacy_num < 0 ? 0 :  100 - $intimacy_num;
        $res['flag_log'] = $intimacy;
        OutJson::toJson(Hint::SUCCESS, '成功', $res);
    }




    public function getAllLiang(Request $request)
    {
        $arr = "198888,196666,192222,197777,190000,190666,192333,196888,201999,222019,192000,192555,775888,246888,131444,111314,191000,192666,193000,194000,195000,196000,197000,198000,190111,190222,190333,190444,190555,190777,190888,365000,201900,201911,201922,201933,201944,201955,201966,177177,201988,201199,131499,131400,131411,131422,131433,131455,131466,131477,131488,221314,331314,441314,551314,661314,771314,881314,991314,110521,220521,330521,440521,550521,365066,365077,365088,365099,365366,212121,520520,521521,686868,168168,268268,886886,123123,966966,989898,365365,111000,242424,535353,120120,233233,110001,191919,654321,123456,456789,345678,234567,567567,135135,246246,789789,520365,191314,201314,520366";
        $liang = explode(',', $arr);
        $data = [];
        for ($i = 0; $i < 100; $i++) {
            $j = $i / 2;
            if (strpos($j, '.')) {
                continue;
            } else {
                $data[$i][] = $liang[$i];
                $data[$i][] = $liang[$i + 1];
            }
        }
        $data = array_values($data);
        OutJson::toJson(200, '成功', $data);
    }

    public function clickGrinLog(Request $request)
    {
        $id = $request->input('user_id') ?? 0;
        $rank = $request->input('rank') ?? 0;
        $res = $this->nationServices->clickGridLogMall($id, $rank);
        OutJson::toJson($res['code'], $res['msg']);
    }


    public function awardGift()
    {
        return $this->nationServices->awardGift();
    }


    public function getRankCache()
    {
        return $this->nationServices->getRankCache();
    }

    public function rechargeDiamondToScore()
    {
       return $this->nationServices->rechargeDiamondToScore();
    }

    public function scoreToUser()
    {
        return $this->nationServices->scoreToUser();
    }


}